<?php

namespace app\model\v1\repair;

use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;

class RepairCatUser extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    public function cat()
    {
        return $this->hasOne(RepairCat::class,'id','cat_id');
    }

    public function rs()
    {
        return $this->hasOne(RepairService::class,'id','rs_id');
    }




    /**
     * 第三方厂商维修工管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getCatUserList($param = [])
    {
        $data["data"] = self::field ("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->with([
                'cat'=> function($query) {
                    $query->field('id,name,status,img');
                },
                'rs'=> function($query) {
                    $query->field('id,uid,nickname,avatar,phone,status,name');
                },
            ])
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
                if(!empty($param["cat_id"])) $query->where("cat_id", $param["cat_id"]);
            })
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
                if(!empty($param["cat_id"])) $query->where("cat_id", $param["cat_id"]);
            })
            ->count();

        return $data;
    }


    /**
     * 第三方厂商维修工管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateCatUser($param = [])
    {
        $find = self::whereNull("delete_time")->where(["cat_id"=>$param["cat_id"], "rs_id"=>$param["rs_id"]])->where("id","<>", $param["id"])->find();
        if(!empty($find)) Throw new Exception("当前分类下的此维修工已存在！");

        $update["update_time"] = time();  //更新时间
        if(!empty($param["cat_id"]))        $update["cat_id"] = $param["cat_id"];
        if(!empty($param["rs_id"]))         $update["rs_id"] = $param["rs_id"];
        if(!empty($param["name"]))          $update["name"] = $param["name"];
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"];

        return self::where("id", $param['id'])->update($update);
    }


    /**
     * 添加第三方厂商维修工
     * @param $param
     * @return int|string
     */
    public static function addCatUser($param = [])
    {
        $find = self::whereNull("delete_time")->where(["cat_id"=>$param["cat_id"], "rs_id"=>$param["rs_id"]])->find();
        if(!empty($find)) Throw new Exception("当前分类下的此维修工已存在！");

        $add = [
            "cat_id"        => $param["cat_id"],
            "rs_id"         => $param["rs_id"] ?? '',
            "name"          => $param["name"] ?? '',
            "sort"          => $param["sort"] ?? '',
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     * 第三方厂商维修工管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableCatUser($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }


    /**
     * 第三方厂商维修工列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRepairCatUserData($param = [])
    {
        return self::field ("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
                when status=2 then '禁用' 
                end as status_text 
            ")
            ->with([
                'cat'=> function($query) {
                    $query->field('id,name,status,img');
                },
                'rs'=> function($query) {
                    $query->field('id,uid,nickname,avatar,phone,status,name');
                },
            ])
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param["cat_id"])){
                    $query->where("cat_id", $param["cat_id"])->where("status", 1);
                }
            })
            ->select()->toArray() ?? [];
    }



}